package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredPermission(name = "权限页面",expremission = "permission:list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){//因为要看到他的数据,所以要放在作用域里面,所以得用到它的addattribute
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<Department> departments = departmentService.listall();
        model.addAttribute("departments",departments);

        return "permission/list";//因为已经配置了视图解析器,所以省略了前面的WEB-INF/views/ 和后面的.jsp
    }

   @RequestMapping("/reload")
   @RequiredPermission(name = "权限加载",expremission = "permission:reload")
   @ResponseBody
    public JsonResult reload(){
       try {
           permissionService.reload();
           return new JsonResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new JsonResult(false,"权限加载失败");
       }
   }
}
